package com.payment.common.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 *
 * @author payment
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	public static String YYYY = "yyyy";

	public static String YYYY_MM = "yyyy-MM";

	public static String YYYY_MM_DD = "yyyy-MM-dd";

	public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
			"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
			"yyyy.MM.dd HH:mm", "yyyy.MM" };

	/**
	 * 获取当前Date型日期
	 *
	 * @return Date() 当前日期
	 */
	public static Date getNowDate() {
		return new Date();
	}

	/**
	 * 获取当前日期, 默认格式为yyyy-MM-dd
	 *
	 * @return String
	 */
	public static String getDate() {
		return dateTimeNow(YYYY_MM_DD);
	}

	public static String getDate1() {
		return dateTimeNow(YYYY_MM);
	}

	public static final String getTime() {
		return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
	}

	public static final String dateTimeNow() {
		return dateTimeNow(YYYYMMDDHHMMSS);
	}

	public static final String dateTimeNow(final String format) {
		return parseDateToStr(format, new Date());
	}

	public static final String dateTime(final Date date) {
		return parseDateToStr(YYYY_MM_DD, date);
	}

	public static final String parseDateToStr(final String format, final Date date) {
		return new SimpleDateFormat(format).format(date);
	}

	public static final Date dateTime(final String format, final String ts) {
		try {
			return new SimpleDateFormat(format).parse(ts);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 只返回当前几号
	 *
	 * @return
	 */
	public static final Integer dateTimeByDay() {
		String dateTime = parseDateToStr(YYYY_MM_DD, new Date());

		return Integer.parseInt(dateTime.substring(dateTime.length() - 2, dateTime.length()));
	}

	/**
	 * 日期路径 即年/月/日 如2018/08/08
	 */
	public static final String datePath() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyy/MM/dd");
	}

	/**
	 * 日期路径 即年/月/日 如20180808
	 */
	public static final String dateTime() {
		Date now = new Date();
		return DateFormatUtils.format(now, "yyyyMMdd");
	}

	public static final String dateTime1(Date now) {
		return DateFormatUtils.format(now, YYYY_MM_DD_HH_MM_SS);
	}

	/**
	 * 日期型字符串转化为日期 格式
	 */
	public static Date parseDate(Object str) {
		if (str == null) {
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取服务器启动时间
	 */
	public static Date getServerStartDate() {
		long time = ManagementFactory.getRuntimeMXBean().getStartTime();
		return new Date(time);
	}

	/**
	 * 计算两个时间差 订单超时时间
	 */
	public static String getDatePoorByMin(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少小时
		// 根据总分钟数 返回指定提示信息
		if (diff > nd) {
			return "已自动取消订单";
		} else {
			// 计算还有多少时间 订单超时
			long dif = nd - diff;
			long hour = dif / nh;
			// 计算差多少分钟
			long min = dif % nd % nh / nm;
			return hour + "小时 " + min + "分钟  ";
		}
	}

	/**
	 * 计算两个时间差
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		long sec = diff % nd % nh % nm / ns;
		return day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
	}

	/**
	 * 计算两个时间差
	 */
	public static String getDateDayAndHour(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		return day + "天" + hour + "小时";
	}

	/**
	 * 获取随机时间
	 *
	 * @param beginDate
	 *            起始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	public static Date randomDate(Long beginDate, Long endDate) {
		try {
			if (beginDate >= endDate) {
				return new Date(endDate);
			}

			long date = random(beginDate, endDate);
			return new Date(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取随机时间戳
	 *
	 * @param begin
	 * @param end
	 * @return
	 */
	public static long random(long begin, long end) {
		long rtn = begin + (long) (Math.random() * (end - begin));
		if (rtn == begin || rtn == end) {
			return random(begin, end);
		}
		return rtn;
	}

	/**
	 * 获取房前日期的前一天
	 *
	 * @return
	 */
	public static final Date dayBefore() {
		Date dNow = new Date(); // 当前时间
		Calendar calendar = Calendar.getInstance(); // 得到日历
		calendar.setTime(dNow);// 把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
		Date dBefore = new Date();
		dBefore = calendar.getTime(); // 得到前一天的时间
		return dBefore;
	}

	/**
	 * 比较日期
	 *
	 * @param dateStr1
	 * @param dateStr2
	 * @return
	 */
	public static int compareDate(String dateStr1, String dateStr2) {
		Date date1 = parseDate(dateStr1);
		Date date2 = parseDate(dateStr2);
		if (date1.getTime() > date2.getTime()) {
			return -1;
		} else if (date1.getTime() < date2.getTime()) {
			return 1;
		} else {
			return 0;
		}

	}

	/**
	 * 比较日期 返回分钟
	 *
	 * @return
	 */
	public static long compareDateByBranch(Date dateStr2) {
		Calendar calendarNow = Calendar.getInstance();
		calendarNow.setTime(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateStr2);
		long val = calendarNow.getTimeInMillis() - calendar.getTimeInMillis();
		return val / 60000;
	}

	/**
	 * 比较日期 返回天数  当前时间减去传入时间
	 *
	 * @return
	 */
	public static int compareDateDay(Date date2) {
		Calendar calendarNow = Calendar.getInstance();
		calendarNow.setTime(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date2);
		long val = calendarNow.getTimeInMillis() - calendar.getTimeInMillis();
		int day = (int) (val / 60000 / 60 / 24);
		return day;
	}
	
	/**
	 * 比较日期 返回天数 传入时间减去当前时间
	 *
	 * @return
	 */
	public static int compareDateDayTwo(Date date2) {
		Calendar calendarNow = Calendar.getInstance();
		calendarNow.setTime(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date2);
		long val = calendar.getTimeInMillis() - calendarNow.getTimeInMillis();
		int day = (int) (val / 60000 / 60 / 24);
		return day;
	}

	public static String timeStamp2Date(String dateStr, String format) {
		Date date = DateUtils.parseDate(dateStr);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	// JAVA获取某段时间内的所有日期
	public static List<String> findDates(String dStart, String dEnd) {
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
		List<String> dateList = null;
		try {
			Calendar cStart = Calendar.getInstance();
			cStart.setTime(sdf.parse(dStart));
			dateList = new ArrayList<String>();
			// 别忘了，把起始日期加上
			dateList.add(dStart);
			// 此日期是否在指定日期之后
			while (sdf.parse(dEnd).after(cStart.getTime())) {
				// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
				cStart.add(Calendar.DAY_OF_MONTH, 1);
				dateList.add(sdf.format(cStart.getTime()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateList;
	}
	
	
	// JAVA获取某段时间内的所有日期
	public static List<String> findPlanDates(String dEnd) {
		SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
		List<String> dateList = null;
		try {
			Calendar cStart = Calendar.getInstance();
			cStart.setTime(getNowDate());
			cStart.add(Calendar.DAY_OF_YEAR, 1);
			dateList = new ArrayList<String>();
			// 别忘了，把起始日期加上
			dateList.add(sdf.format(cStart.getTime()));
			// 此日期是否在指定日期之后
			while (sdf.parse(dEnd).after(cStart.getTime())) {
				// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
				cStart.add(Calendar.DAY_OF_MONTH, 1);
				dateList.add(sdf.format(cStart.getTime()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateList;
	}
}
